﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entity;
using Utility;

namespace StudentAppointmentPlatform
{
    public partial class RecordDetail : System.Web.UI.Page
    {
        private AppointmentRecord record;

        protected void Page_Load(object sender, EventArgs e)
        {
            string idStr = Request.QueryString["AppointmentId"];
            int appointmentId;

            if (int.TryParse(idStr, out appointmentId))
            {
                record = RecordBO.GetRecordDetail(appointmentId);
            }
            else
            {
                Alert.AlertOnPage(Page, "未找到此记录！即将返回。");
                Redirect.Js_Redirect(Page, "/ShowHistory.aspx");
            }

            if (!IsPostBack)
            {
                lbApplicant.Text = UserBO.GetUserNameAndUserId(record.ApplicantId);
                lbCreatTime.Text = record.CreatTime.ToString("yyyy年MM月dd日 HH:mm:ss");
                lbServiceName.Text = record.Service.ServiceName;
                lbDay.Text = record.Day;
                lbTimeBucket.Text = TimeBucket.ChangeTimeBucketToShowTime(record.TimeBucket);
                lbStatus.Text = record.Status;

                string approvalStatus = record.ApprovalStatus;
                if (approvalStatus != null)
                {
                    if (approvalStatus == AppointmentRecord.RecordStatus.Approve.ToString())
                    {
                        rbApprove.Checked = true;
                    }
                    else if (approvalStatus == AppointmentRecord.RecordStatus.Reject.ToString())
                    {
                        rbReject.Checked = true;
                    }
                }

                lbDirectorName.Text = record.Service.Director.UserName;
                if (record.ApproveTime.ToString("yyyy") == "0001")
                {
                    lbApproveTime.Text = "（暂未审批）";
                }
                else
                {
                    lbApproveTime.Text = record.ApproveTime.ToString("yyyy年MM月dd日 HH:mm:ss");
                }
                tbComment.Text = record.Comment;
                tbApproval.Text = record.ApprovalComment;

                setCommandVisual();
                
            }
            
        }

        /// <summary>
        /// 设置评论区/审批区元素的可视化情况;
        /// </summary>
        private void setCommandVisual()
        {
            if (Session["LoginType"].ToString() == "Student")
            {
                if (record.Status == AppointmentRecord.RecordStatus.Approve.ToString() ||
                    record.Status == AppointmentRecord.RecordStatus.Reject.ToString())
                {
                    tbComment.Enabled = true;
                    btnComment.Visible = true;
                }
            }
            else if (Session["LoginType"].ToString() == "Director")
            {
                if ((int) RecordBO.FindRecordStatus(record.Status) <= (int) AppointmentRecord.RecordStatus.Apply)
                { // 还未审批过;
                    tbApproval.Enabled = true;
                    rbApprove.Enabled = true;
                    rbReject.Enabled = true;
                    btnApproval.Visible = true;
                }
            }
        }

        protected void btnBack_OnClick(object sender, EventArgs e)
        {
            Redirect.Js_Redirect2(Page, "/ShowHistory.aspx");
        }

        protected void btnApproval_OnClick(object sender, EventArgs e)
        {
            string status;
            if (rbApprove.Checked)
            {
                status = AppointmentRecord.RecordStatus.Approve.ToString();
            }
            else if (rbReject.Checked)
            {
                status = AppointmentRecord.RecordStatus.Reject.ToString();
            }
            else
            {
                Alert.AlertOnPage2(Page, "请您务必选择一个审批状态.");
                return;
            }

            string approvalComment = tbApproval.Text;

            if (RecordBO.UpdateRecordStatusByDirector(record.AppointmentId, status, approvalComment) > 0)
            {
                Alert.AlertOnPage2(Page, "审批成功!");

                Message msg = new Message();
                msg.UserId = record.ApplicantId;
                msg.MessageContent = @"您对 " + record.Service.ServiceName + " 的预约已被审批，请查看：<a href='/RecordDetail.aspx?AppointmentId=" + record.AppointmentId + "'>详情</a>";
                MessageBO.InsertMessage(msg);
            }
            else
            {
                Alert.AlertOnPage2(Page, "出错了!");
            }
            Refresh.RefreshCurrentPage(Page);

        }

        protected void btnComment_OnClick(object sender, EventArgs e)
        {
            string comment = tbComment.Text;

            if (RecordBO.StudentMakeComment(record.AppointmentId, comment) > 0)
            {
                Alert.AlertOnPage2(Page, "评价成功!");
            }
            else
            {
                Alert.AlertOnPage2(Page, "操作失败!");
            }
            Refresh.RefreshCurrentPage(Page);
        }
    }
}